let sqlite3 = require('sqlite3');

class Sqlite{
    constructor(path){
        this.sql3 = new sqlite3.Database(path, function(e){
            if (e) console.log('sqlite3 open db error!');
        });
    }
    queryOne(sql){
        let db = this.sql3;
        return new Promise(function (resolve, reject) {
            db.get(sql, function(err, row){
                if (err) {
                    reject(err);
                } else {
                    resolve(row);
                }
            });
        })
    }
    queryAll(sql){
        let db = this.sql3;
        return new Promise(function (resolve, reject) {
            db.all(sql, function(err, row){
                if (err) {
                    reject(err);
                } else {
                    resolve(row);
                }
            });
        })
    }
    execute(sql){
        let db = this.sql3;
        return new Promise(function (resolve, reject) {
            db.run(sql, function(err){
                if (err) {
                    reject(err);
                } else {
                    resolve(true);
                }
            });
        })
    }
}

// let s3 = new Sqlite("./userdata.db");
// s3.queryAll("SELECT * FROM tb_account").then((row)=>{
//     console.log(row);
// }).catch((err)=>{
//     console.log(err);
// });

module.exports = Sqlite;